home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3156 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Adding Arrays, anyone?
  5. Date: 26 Jan 1996 15:38:03 GMT
  6. Organization: Los Alamos National Laboratory
  7. Distribution: world
  8. Message-ID: <TANMOY.96Jan26083803@qcd.lanl.gov>
  9. References: <8B97599.014F009D28.uuout@filebank.cts.com>
  10.     <8B9852B.014F009DE3.uuout@filebank.cts.com>
  11. NNTP-Posting-Host: qcd.lanl.gov
  12. Mime-Version: 1.0
  13. Content-Type: text
  14. In-reply-to: jack.greene@filebank.cts.com's message of Thu, 25 Jan 96 22:03:00 -0800
  15.  
  16. In article <8B9852B.014F009DE3.uuout@filebank.cts.com>
  17. jack.greene@filebank.cts.com (JACK GREENE) writes: 
  18. <snip>
  19.    RK>  I tried something like
  20.    RK>  
  21.    RK>  for(counter =0 ; counter != '\0'; counter++)
  22.    RK>   printf("%c",string1[counter] + string2[counter]
  23.    RK>  
  24.    RK>  and I got a bunch of garbage on the screen.
  25.  
  26.    :-) And I bet it didn't compile very well either :-)
  27.  
  28. Why do you say that? The missing ); is obviously a typo: is that what
  29. you are talking about?
  30.  
  31.    When adding strings together like you've shown above, C will
  32.    concatenate as opposed to doing the math that you're asking
  33.  
  34. What do you mean by concatenate in this context? I can't see any
  35. concatenation going on there?
  36.  
  37.     it to do.  Try converting to  a numeric variable to
  38.    add.  I would try something like this:
  39.  
  40.        for (counter = 0; counter != '0'; counter++) {
  41.      printf("%c", itoa( 
  42.           atoi( string1[counter] ) + 
  43.           atoi(string2[counter]) )
  44.      };
  45.  
  46.    The functions:
  47.     atoi()   /* ascii to integer */
  48.     itoa()   /* integer to ascii */
  49.  
  50.    are in the STDLIB.H in borlands cpp4.51
  51.  
  52. In every standard conforming compiler, <stdlib.h> has to declare
  53. atoi. In no standard conforming compiler can it declare itoa.
  54.  
  55. atoi needs strings. I assume string1[counter] is a char, not a
  56. string. atoi cannot be legally passed a char (or int that results from
  57. the promotion of a char). Did you try your solution before posting?
  58.  
  59. Also, atoi can be used to interpret character strings as numbers even
  60. on machines which do not use ascii.
  61.  
  62.    Of course somebody else might have it a little neater and I wouldn't
  63.    mind seeing that either.
  64.    The main idea is to convert the strings to integers.  You could even
  65.    change the printf() to use
  66.    "%d" and not even bother with the itoa().
  67.  
  68. Actually, I thought the question was of converting _characters_ to
  69. integers: not strings to integers. A string is a sequence of
  70. characters terminated by '\0', and it seemed the original poster
  71. wanted a sequence of integers (not one integer) from a sequence of
  72. characters. 
  73.  
  74. In any case, as digits are guaranteed to be contiguous in any code
  75. used by a C implementation, string1[counter]-'0' gives the int you
  76. want (provided of course string1[counter] is between '0' and
  77. '9'). Similarly, if x is any int between 0 and 9, '0'+x is the
  78. corresponding character.
  79.  
  80.    Just a thought from an old BASIC and xBase hack learning C myself.
  81.  
  82. Happy hacking ...
  83.  
  84. Cheers
  85. Tanmoy
  86. --
  87. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  88. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  89. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  90. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  91. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  92. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  93.